﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ZombieMonkeys.Vibria
{
    public partial class NoteSquareControl : UserControl
    {
        Logic.Square square;
        int row;
        int col;
        int noteID;

        NoteGrid noteGrid;

        public NoteSquareControl(int r, int c, int i, Logic.Square s)
        {
            square = s;
            row = r;
            col = c;
            noteID = i;
            InitializeComponent();

            DrawBorder();
        }

        /// <summary>
        /// reference to the instance of puzzle grid that created the square
        /// </summary>
        public NoteGrid GetNoteGrid
        {
            set
            {
                noteGrid = value;
            }
        }

        //text value to show in the square
        private string noteValue;
        public string NoteValue
        {
            get
            {
                return noteValue;
            }
            set
            {
                if (value == "0")
                {
                    noteValue = "";
                    square.SetNote(noteID, 0);
                }
                else
                {
                    noteValue = value;
                    square.SetNote(noteID, int.Parse(value));
                }
                squareControlText.Text = noteValue;
            }
        }
        
        //the selected square can accept numbers from the button grid
        bool selected;
        public bool Selected
        {
            get
            {
                return selected;
            }
            set
            {
                selected = value;
                SquareShading();
            }
        }

        /// <summary>
        /// Square shading method to call when there is a change in the square's state
        /// </summary>
        private void SquareShading()
        {
            if (selected)
            {
                squareControlText.Foreground = (SolidColorBrush)Resources["PhoneDisabledBrush"];
                LayoutRoot.Background = (SolidColorBrush)Resources["PhoneAccentBrush"];
            }
            else
            {
                squareControlText.Foreground = (SolidColorBrush)Resources["PhoneAccentBrush"];
                LayoutRoot.Background = (SolidColorBrush)Resources["PhoneBackgroundBrush"];
            }
        }

        /// <summary>
        /// Draw the borders to look like a regular sudoku grid
        /// </summary>
        private void DrawBorder()
        {
            int top = 1;
            int right = 1;
            int left = 1;
            int bottom = 1;
            if (this.row == 0)
                top = 3;
            if (this.col == 0)
                left = 3;
            if (this.row == 1)
                bottom = 3;
            if (this.col == 1)
                right = 3;

            SquareControlBorder.BorderThickness = new Thickness(left, top, right, bottom);
        }

        /// <summary>
        /// Event handler for when the square is clicked
        /// </summary>
        public event EventHandler SquareClicked;

        /// <summary>
        /// Called when a square is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (SquareClicked != null)
            {
                SquareClicked(this, null);
                selected = true;
                SquareShading();
            }
        }
    }
}
